package GUI.TabPanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.IntegerComparator;

import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class ViewGuestsPanel extends JPanel {
	private JLabel bidLabel = new JLabel("Booking ID:");
	private JTextField bidText = new JTextField(9);

	private JLabel firstNameLabel = new JLabel("First Name:");
	private JTextField firstNameText = new JTextField(9);

	private JLabel lastNameLabel = new JLabel("Last Name:");
	private JTextField lastNameText = new JTextField(9);

	private JLabel emailLabel = new JLabel("Email:");
	private JTextField emailText = new JTextField(9);

	private JLabel blank = new JLabel(" ");
	private JButton search;
	private JButton add_fees = new JButton("Add Fees");

	private JTextField description = new JTextField(15);
	private JTextField cost = new JTextField(15);

	private JTable informationTable;

	//TableRowSorter<DefaultTableModel> sorter;

	private String[] columnNames =
	{
			"Booking #", "First Name", "Last Name", "Email",
			"Room #", "Date In", "Date Out", "Cost", "Status", "Hotel Name" 
	};

	public ViewGuestsPanel(){
		this.setLayout(new BorderLayout());



		JPanel west = new JPanel();
		JPanel center = new JPanel();
		JPanel east = new JPanel();

		west.setBackground(new Color(147, 37, 37));
		west.add(new JLabel("      "));
		east.setBackground(new Color(147, 37, 37));
		east.add(new JLabel("      "));

		center.setLayout(new GridLayout(0, 2, -300, 7));

		search = new JButton("Search");	

		center.add(bidLabel);
		center.add(bidText);
		center.add(firstNameLabel);
		center.add(firstNameText);
		center.add(lastNameLabel);
		center.add(lastNameText);
		center.add(emailLabel);
		center.add(emailText);
		center.add(blank);

		search.addActionListener(new SearchButton());
		add_fees.addActionListener(new AddFeesButton());

		JPanel buttons = new JPanel();
		buttons.add(search);
		buttons.add(add_fees);

		center.add(buttons);

		center.add(new JLabel("Fee Description"));

		center.add(description);
		center.add(new JLabel("Fee Cost"));
		center.add(cost);

		this.add(west, BorderLayout.WEST);
		this.add(center, BorderLayout.CENTER);
		this.add(east, BorderLayout.EAST);

		SetUpTable();
	}

	public void SetUpTable(){
		Object[][] tableData = Controller.GetBookings(null, "CHECKED_IN");

		CreateTable(tableData);
		informationTable.setVisible(false);
		informationTable.setVisible(true);
	}

	void CreateTable(Object[][] tableData){
		informationTable = new JTable(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false; 
			} 
		};
		informationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(0, new IntegerComparator());
		sorter.setComparator(5, new IntegerComparator());

		informationTable.setRowSorter(sorter);

		informationTable.setPreferredScrollableViewportSize(new Dimension(700, 320));
		informationTable.setFillsViewportHeight(true);

		JScrollPane scrollPane = new JScrollPane(informationTable);
		this.add(scrollPane, BorderLayout.SOUTH);
	}

	private class SearchButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {

			DefaultTableModel model = new DefaultTableModel(Controller.GetBookings(null, "CHECKED_IN"), columnNames);

			informationTable.setModel(model);

			RowFilter<Object, Object> filter = new RowFilter<Object, Object>()
			{
				public boolean include(Entry<?, ?> entry)
				{
					return (((0 == entry.getStringValue(0).compareTo(bidText.getText())) || bidText.getText().length() < 1)
							&& ((0 == entry.getStringValue(1).compareTo(firstNameText.getText())) || firstNameText.getText().length() < 1)
							&& ((0 == entry.getStringValue(2).compareTo(lastNameText.getText())) || lastNameText.getText().length() < 1)
							&& ((0 == entry.getStringValue(3).compareTo(emailText.getText())) || emailText.getText().length() < 1));
				}
			};

			TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);
			sorter.setRowFilter(filter);
			if (bidText.getText().length() < 1 
					&& firstNameText.getText().length() < 1
					&& lastNameText.getText().length() < 1
					&& emailText.getText().length() < 1)
				sorter.setRowFilter(null);
			informationTable.setRowSorter(sorter);

			//SetUpTable();
			//Update();
		}
	}
	/*
	private class AddFeesButton implements ActionListener{
		private JTextField cost = new JTextField(15);
		private JTextField description = new JTextField(15);
		private JButton add_fee = new JButton("Add Fee");

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JDialog fees_menu = new JDialog();		

			fees_menu.setLayout(new GridLayout(0,2));

			fees_menu.add(new JLabel("Description:"));
			fees_menu.add(description);
			fees_menu.add(new JLabel("Cost:"));
			fees_menu.add(cost);

			add_fee.addActionListener(new ConfirmFeeButton());
			fees_menu.add(add_fee);

		}*/

	private class AddFeesButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			if(informationTable.getSelectedRow()!=-1){
				try{
					Integer.parseInt(cost.getText());
					String booking_ID = Controller.GetBookings(null, "CHECKED_IN")[informationTable.getSelectedRow()][0];
					Controller.AddCost(booking_ID, cost.getText(), description.getText());
					JOptionPane.showMessageDialog(null, "Fee Added!");
				}catch(NumberFormatException ex){
					JOptionPane.showMessageDialog(null, "Fee Must Be An Integer");
				}
				
			}else{
				JOptionPane.showMessageDialog(null, "Please Select A Guest.");
			}
		}

	}

	//}

	public void Update(){
		System.out.println("updating view guests");
		this.setVisible(false);
		this.setVisible(true);
	}

	public void UpdateTable(){
		new SearchButton().actionPerformed(null);
	}
}
